package 贪心;

import java.util.Arrays;

/**
 * TODO
 *
 * @Description
 * @Author Lx
 * @Date 2024/7/21 2:33
 **/
public class 贪心9_分发糖果 {
    public int candy(int[] ratings) {
        int[] count=new int[ratings.length];
        int[] scope =new int[ratings.length];
        //1.就是比较相邻的，有比一个高就能多要一个。我们在每一分加完后去判断相邻的

        //1.find max
        int maxC=0;
        for (int i = 0; i < ratings.length; i++) {
            if(ratings[i]>maxC)maxC=ratings[i];
        }
        //遍历数组O(N)*遍历值O(MAX_C)
        System.out.println("maxC="+maxC);
        for(int i=0;i<maxC;i++){//遍历maxC,maxC=总共要加分的最多次数
            System.out.println("i:"+i+"-------------------------");
            //1.每加分
            for (int j = 0; j < ratings.length; j++) {//遍历ratings
                //给数组每个元素一分
                if(ratings[j]>0&&scope[j]!=Integer.MAX_VALUE){
                    scope[j]+=1;
                    ratings[j]--;
                }
            }
            System.out.println("scope："+Arrays.toString(scope));
            //2.每加完分之后去判断相邻的
            for (int j = 0; j < ratings.length; j++) {
                //如果比相邻的一个高，给其加一个糖果
                if(j==0){
                    if(scope[j]>scope[j+1]){
                        count[j]+=1;
                        scope[j+1]=Integer.MAX_VALUE;
                    }
                }else if(j==ratings.length-1){
                    if(scope[j]>scope[j-1]){
                        count[j]+=1;
                        scope[j-1]=Integer.MAX_VALUE;
                    }
                }else{
                    if(scope[j]>scope[j-1]||scope[j]>scope[j+1]){
                        count[j]+=1;
                        scope[j-1]=scope[j+1]=Integer.MAX_VALUE;
                    }
                }
            }
            System.out.println("count："+Arrays.toString(count));
        }
        int sum=0;
        for (int i = 0; i < count.length; i++) {
            sum+=count[i];
        }
        return sum;
    }
    public static void main(String[] args) {
        System.out.println(new 贪心9_分发糖果().candy(new int[]{1,0,2}));
    }
}
